<?php

namespace Core\Components;

class DBUtils
{
    private $host, $port, $user, $password, $dbname;
    private $conn;
    public function __construct($arr = array())
    {
        $this->host = isset($arr['host']) ? $arr['host'] : '127.0.0.1'; //先判断是否有自己的host，如果有就用自己的host，否则就使用默认的localhost
        $this->port = isset($arr['port']) ? $arr['port'] : '3306';
        $this->user = isset($arr['user']) ? $arr['user'] : 'root';
        $this->password = isset($arr['pass']) ? $arr['pass'] : '3edcRFVwangcl';
        $this->dbname = isset($arr['dbname']) ? $arr['dbname'] : 'blog';
        $this->connect();
    }
    private function connect()
    {
        $this->conn = mysqli_connect($this->host, $this->user, $this->password, $this->dbname);
        if ($this->conn && $this->conn->connect_errno) {
            echo "数据库连接错误<br>";
            exit;
        }
        //echo "数据库连接成功<br>";
    }
    public function db_query($sql)
    {
        $res = mysqli_query($this->conn, $sql);
        return $res;
    }
}
